using System.Windows;
using ErpManager.WPF.ViewModels;
using ErpManager.WPF.Services;

namespace ErpManager.WPF.Views
{
    public partial class RegisterWindow : Window
    {
        private readonly RegisterViewModel _viewModel;

        public RegisterWindow(AuthenticationService authService)
        {
            InitializeComponent();
            
            _viewModel = new RegisterViewModel(authService);
            DataContext = _viewModel;

            // 处理PasswordBox的密码绑定
            PasswordBox.PasswordChanged += (s, e) =>
            {
                _viewModel.Password = PasswordBox.Password;
            };

            ConfirmPasswordBox.PasswordChanged += (s, e) =>
            {
                _viewModel.ConfirmPassword = ConfirmPasswordBox.Password;
            };

            // 注册成功后关闭窗口
            _viewModel.RegistrationSuccessful += () =>
            {
                MessageBox.Show("注册成功！请使用新账号登录。", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
                DialogResult = true;
                Close();
            };

            // 取消按钮关闭窗口
            _viewModel.CancelCommand = new RelayCommand(() =>
            {
                DialogResult = false;
                Close();
            });
        }
    }
}
